package ufc.androidprint;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import ufc.androidprint.FileItem.ICONTYPE;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidPrintActivity extends Activity{

	private enum DISPLAYMODE {
		ABSOLUTE, RELATIVE;
	}

	private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
	private List<FileItem> directoryEntries = new ArrayList<FileItem>();
	private File currentDirectory = new File("/");
	private static FileItem clickedFile;
	


	public static FileItem getClickedFile() {
		return clickedFile;
	}

	public static void setClickedFile(FileItem clickedFile) {
		AndroidPrintActivity.clickedFile = clickedFile;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		browseToPDFDirectory();

	}

	private void browseToPDFDirectory() {
		browseTo(new File("/"));
	}

	private void browseTo(final File aDirectory) {
		if (aDirectory.isDirectory()) {
			this.currentDirectory = aDirectory;
			fill(aDirectory.listFiles());
		}
	}

	private void fill(File[] files) {

		this.directoryEntries.clear();

		switch (this.displayMode) {
			case ABSOLUTE:
				for (File file : files) {
					this.directoryEntries.add(new FileItem(false, ICONTYPE.PDF, file.getPath()));
				}
				break;
			case RELATIVE: // On relative Mode, we have to add the current-path to
							// the beginning
				int currentPathStringLenght = this.currentDirectory
						.getAbsolutePath().length();
				for (File file : files) {
					this.directoryEntries.add(
							new FileItem(false, ICONTYPE.PDF, file.getAbsolutePath().substring(
									currentPathStringLenght)));

				}
				break;
		}

		ListView lv = (ListView) findViewById(R.id.fileListView);			
		lv.setAdapter(new FileListAdapter(this, this.directoryEntries));
		
	}


	public void handleButtonClick(View v) {
		if (v.getId() == R.id.printButton) {
			Toast.makeText(this,
					"Arquivo selecionado: " + clickedFile.getName() ,
					Toast.LENGTH_LONG).show();
		}
	}


}